extends KinematicBody2D

signal life_changed(lives)
signal dead

enum {IDLE,RUN,JUMP,HURT,DEAD}

export var run_speed=150
export var jump_speed=-300
export var gravity=750


var state 
var anim
var new_anim
var velocity=Vector2()
var life

func _ready() -> void:
	change_state(IDLE)
	
func change_state(new_state):
	match new_state:
		IDLE:
			new_anim='idle'
		RUN:
			new_anim='run'
		JUMP:
			new_anim='jump_up'
		HURT:
			new_anim='hurt'
			velocity.y=-200
			velocity.x=-100*sign(velocity.x)
			life-=1
			emit_signal('life_changed',life)
			yield(get_tree().create_timer(.5),'timeout')
			change_state(IDLE)
			if life<=0:
				change_state(DEAD)
		DEAD:
			emit_signal('dead')
			hide()
	state= new_state

func _physics_process(delta: float) -> void:
	velocity.y+=gravity*delta
	get_input()
	if new_anim!=anim:
		anim=new_anim
		$AnimationPlayer.play(anim)
	
	velocity=move_and_slide(velocity,Vector2(0,-1))
	if state==JUMP and is_on_floor():
		change_state(IDLE)
	if state==JUMP and velocity.y>0:
		new_anim='jump_down'

func get_input():
	if state==HURT:
		return
	var right=Input.is_action_pressed('right')
	var left=Input.is_action_pressed('left')
	var jump=Input.is_action_just_pressed('jump')
	velocity.x=0
	if right:
		velocity.x+=run_speed
		$Sprite.flip_h=false
	if left:
		velocity.x-=run_speed
		$Sprite.flip_h=true
	if jump and is_on_floor():
		change_state(JUMP)
		velocity.y=jump_speed
	if state==IDLE and velocity.x!=0:
		change_state(RUN)
	if state==RUN and velocity.x==0:
		change_state(IDLE)
	if state in [IDLE,RUN] and !is_on_floor():
		change_state(JUMP)

func start(pos):
	position=pos
	show()
	change_state(IDLE)
	life=3
	emit_signal('life_changed',life)

func hurt():
	if state!=HURT:
		change_state(HURT)
